var instdata = "";
var posdata = "";
function initial(){
	// if (sessionStorage.loginclerkname == null){
	// 	window.location='login.html';
	// }
    /**
    JSON.stringify()是将JavaScript对象转换为json字符串
  	JSON.parse(jsonstr)是将json字符串转换为javascript对象 
  	*/
  	$.ajax({
        url:"http://localhost/PersonManSystem/inst/install.action",
        type:"get",
        dataType:'json',
        contentType:'application/json;charset=utf-8',
        //data:JSON.stringify(schoolBannerInfo),
       	//data:schoolBannerInfo,//get方式
       	//data:null,
        success:function(data){
        	instdata = data;
            //alert(JSON.stringify(data));
            //alert(data['data']['一级机构'][0].instname);
            initLoadData('inst', data, '1');
            initLoadData('inst', data, '2');
            initLoadData('inst', data, '3');
        },
        error:function(XMLHttpRequest, textStatus, errorThrown){  
          	alert("Error");  
         	alert(XMLHttpRequest.status);  
          	alert(XMLHttpRequest.readyState);  
          	alert(textStatus); 
        }
  	});   

  	$.ajax({
        url:"http://localhost/PersonManSystem/pos/all.action",
        type:"get",
        dataType:'json',
        contentType:'application/json;charset=utf-8',
        //data:JSON.stringify(schoolBannerInfo),
       	//data:schoolBannerInfo,//get方式
       	//data:null,
        success:function(data){
        	posdata = data;
            //alert(JSON.stringify(data));
            //alert(data['data']['一级机构'][0].instname);
            initLoadData('pos', data, '1');
            initLoadData('pos', data, '2');
            initLoadData('pos', data, '3');
            querySalarystandard($("select[name='thirdposition']").find("option:selected").text());
        },
        error:function(XMLHttpRequest, textStatus, errorThrown){  
          	alert("Error");  
         	alert(XMLHttpRequest.status);  
          	alert(XMLHttpRequest.readyState);  
          	alert(textStatus); 
        }
  	});   
}

/*初始化机构*/
function initLoadData(arg, data, type){
	if (type == '1') {
		if (arg == 'inst') {
			var firstInstSelect = $("#firstinst");
			$(data['data']['一级机构']).each(function(){
				firstInstSelect.append("<option value='" + this.instid + "'>" + this.instname + "</option>");
			});
		} else {
			var firstpositionSelect = $("select[name = 'firstposition']");
			$(data['data']['职位分类']).each(function(){
				firstpositionSelect.append("<option value='" + this.posid + "'>" + this.posname + "</option>");
			});
		}
	}
	else if (type == '2') {
		if (arg == 'inst') {
			var secondInstSelect = $("#secondinst");
			var firstinstid = ($("#firstinst").val());
			$(data['data']['二级机构']).each(function(){
				if (this.instpid == firstinstid) {
					secondInstSelect.append("<option value='" + this.instid + "'>" + this.instname + "</option>");
				}
			});	
		} else {
			var secondpositionSelect = $("select[name = 'secondposition']");
			var firstposid = $("select[name = 'firstposition']").val();
			$(data['data']['职位名称']).each(function(){
				if (this.pospid == firstposid) {
					secondpositionSelect.append("<option value='" + this.posid + "'>" + this.posname + "</option>");
				}
			});	
		}
	} else {
		if (arg == 'inst') {
			var thirdInstSelect = $("#thirdinst");
			var secondinstid = ($("#secondinst").val());
			$(data['data']['三级机构']).each(function(){
				if (this.instpid == secondinstid) {
					thirdInstSelect.append("<option value='" + this.instid + "'>" + this.instname + "</option>");
				}
			});		
		} else {
			var thirdpositionSelect = $("select[name = 'thirdposition']");
			var secondposid = $("select[name = 'secondposition']").val();
			$(data['data']['职称']).each(function(){
				if (this.pospid == secondposid) {
					thirdpositionSelect.append("<option value='" + this.posid + "'>" + this.posname + "</option>");
				}
			});		
		}
	}
}

/*为页面加载点击添加事件*/
function loadBind() {
	/*为一级机构添加点击事件*/
	$("#firstinst").unbind("click");
	$("#firstinst").bind("click", function() {
		clickChange('inst', instdata, '1');
		clickChange('inst', instdata, '2');
		//alert($("select[name='secondinst']").find("option:selected").text());
	});
	/*为二级机构点击添加事件*/
	$("#secondinst").unbind("click");
	$("#secondinst").bind("click", function() {
		clickChange('inst', instdata, '2');
	});
	/*为职位分类添加鼠标点击事件*/
	$("select[name = 'firstposition']").unbind('click');
	$("select[name = 'firstposition']").bind('click', function(){
		clickChange('pos', posdata, '1');
		clickChange('pos', posdata, '2');
		querySalarystandard($("select[name='thirdposition']").find("option:selected").text());
	});
	/*为职位名称添加鼠标点击事件*/
	$("select[name = 'secondposition']").unbind('click');
	$("select[name = 'secondposition']").bind('click', function(){
		clickChange('pos', posdata, '2');
		querySalarystandard($("select[name='thirdposition']").find("option:selected").text());
	});
	/*为职称添加鼠标点击事件*/
	$("select[name = 'thirdposition']").unbind("click");
	$("select[name = 'thirdposition']").bind("click", function(){
		querySalarystandard($(this).find("option:selected").text());
	});
	/*为提交添加点击事件*/
	$("#formsubmit").unbind("click");
	$("#formsubmit").bind("click", function() {
		submitData();
	});
	/*上传头像*/
	$("input[name = 'uploadphoto']").unbind("click");
	$("input[name = 'uploadphoto']").bind("click", function() {

		if (typeof(sessionStorage.clerkname)=="undefined"){
			alert("请先登记用户，再为用户上传头像");
		} else {
			document.uploadhead.action = "http://localhost/PersonManSystem/PersonManSystemHtml/head/upload/"+sessionStorage.clerkname+".action";
			document.uploadhead.submit();
		}
		sessionStorage.removeItem("clerkname");		
	});
}

/*当上一级机构发生变化时，下一级机构跟着改变*/
function clickChange(arg, data, type){

	if (type == '1') {
		if (arg == 'inst') {
			var secondInstSelect = $("#secondinst");
			secondInstSelect.empty();
			var firstinstid = ($("#firstinst").val());
			$(data['data']['二级机构']).each(function(){
				if (this.instpid == firstinstid) {
					secondInstSelect.append("<option value='" + this.instid + "'>" + this.instname + "</option>");
				}
			});	
		} else {
			var secondpositionSelect = $("select[name = 'secondposition']");
			secondpositionSelect.empty();
			var firstposid = $("select[name = 'firstposition']").val();
			$(data['data']['职位名称']).each(function(){
				if (this.pospid == firstposid) {
					secondpositionSelect.append("<option value='" + this.posid + "'>" + this.posname + "</option>");
				}
			});	
		}
	} 
	if (type == '2') {
		if (arg == 'inst') {
			var thirdInstSelect = $("#thirdinst");
			thirdInstSelect.empty();
			var secondinstid = ($("#secondinst").val());
			$(data['data']['三级机构']).each(function(){
				if (this.instpid == secondinstid) {
					thirdInstSelect.append("<option value='" + this.instid + "'>" + this.instname + "</option>");
				}
			});		
		} else {
			var thirdpositionSelect = $("select[name = 'thirdposition']");
			thirdpositionSelect.empty();
			var secondposid = $("select[name = 'secondposition']").val();
			$(data['data']['职称']).each(function(){
				if (this.pospid == secondposid) {
					thirdpositionSelect.append("<option value='" + this.posid + "'>" + this.posname + "</option>");
				}
			});	
		}
	}
}

function submitData(){
	var bookername = $("input[name='bookername']").val();
	if (bookername == "") {
		bookername = sessionStorage.loginclerkname;
	}
	 //js对象
	var recordInfo = {
      	"instname" 			: $("#thirdinst").find("option:selected").text(),
		"posname" 			: $("select[name='thirdposition']").find("option:selected").text(),
		"clerkname" 		: $("input[name='clerkname']").val(),
		"clerksex" 			: $("select[name='clerksex']").find("option:selected").text(),
		"email" 			: $("input[name='email']").val(),
		"mobilephone" 		: $("input[name='mobilephone']").val(),
		"qq"				: $("input[name='qq']").val(),
		"telephone" 		: $("input[name='telephone']").val(),
		"address" 			: $("input[name='address']").val(),
		"postcode" 			: $("input[name='postcode']").val(),
		"nationality" 		: $("input[name='nationality']").val(),
		"birthplace" 		: $("input[name='birthplace']").val(),
		"birthday" 			: $("input[name='birthday']").val(),
		"nation" 			: $("input[name='nation']").val(),
		"faith" 			: $("select[name='faith']").find("option:selected").text(),
		"politicalstatus" 	: $("select[name='politicalstatus']").find("option:selected").text(),
		"idnum" 			: $("input[name='idnum']").val(),
		"socialsecuritynum" : $("input[name='socialsecuritynum']").val(),
		"age" 				: $("input[name='age']").val(),
		"education" 		: $("select[name='education']").find("option:selected").text(),
		"educationyears" 	: $("input[name='educationyears']").val(),
		"major" 			: $("input[name='major']").val(),
		"salarystandardname": $("input[name='salarystandardname']").val(),
		"accountbank" 		: $("input[name='accountbank']").val(),
		"accountid" 		: $("input[name='accountid']").val(),
		"bookername" 		: bookername,//数据库中为bookerid登记人编号;
		"speciality" 		: $("input[name='speciality']").val(),
		"hobby" 			: $("input[name='hobby']").val(),
		"resume" 			: $("textarea[name='resume']").val(),
		"familyinfo" 		: $("textarea[name='familyinfo']").val(),
		"note" 				: $("textarea[name='note']").val()
    };

     /**
    JSON.stringify()是将JavaScript对象转换为json字符串
  	JSON.parse(jsonstr)是将json字符串转换为javascript对象 
  	*/
  	$.ajax({
        url:"http://localhost/PersonManSystem/record/insert/record.action",
        type:"post",
        dataType:'json',
        contentType:'application/json;charset=utf-8',
        data:JSON.stringify(recordInfo),
        success:function(yy){
        	if (yy.msg == '成功') {
        		alert("提交成功！");
        		sessionStorage.setItem("clerkname", $("input[name='clerkname']").val());  
        	}     	  
        },
        error:function(XMLHttpRequest, textStatus, errorThrown){  
          	alert("Error");  
         	alert(XMLHttpRequest.status);  
          	alert(XMLHttpRequest.readyState);  
          	alert(textStatus); 
        }
  	});   
}

function querySalarystandard(posname){
	$.ajax({
        url:"http://localhost/PersonManSystem/pos/query/salarystandard/"+posname+".action",
        type:"get",
        dataType:'json',
        contentType:'application/json;charset=utf-8',
        //data:posnamevalue,
        success:function(data){
        	$("input[name='salarystandardname']").val(data['data']['salarystandardname']);
        },
        error:function(XMLHttpRequest, textStatus, errorThrown){  
          	alert("Error");  
         	alert(XMLHttpRequest.status);  
          	alert(XMLHttpRequest.readyState);  
          	alert(textStatus); 
        }
  	});   
}